# for M&B v.901

from header_common import *
from header_operations import *
from header_parties import *
from header_items import *
from header_skills import *
from header_triggers import *
from header_troops import *
from header_colors import *

from module_constants import *

####################################################################################################################
# Simple triggers are the alternative to old style triggers. They do not preserve state, and thus simpler to maintain.
#
#  Each simple trigger contains the following fields:
# 1) Check interval: How frequently this trigger will be checked
# 2) Operation block: This must be a valid operation block. See header_operations.py for reference. 
####################################################################################################################

# time intervals
hourly = 1
daily = 24
weekly = daily * 7
monthly = daily * 30


simple_triggers = [
#Auto-menu
  (0,
   [
       (map_free),
       (try_begin),
         (ge,"$auto_menu",1),
         (jump_to_menu,"$auto_menu"),
         (assign,"$auto_menu",-1),
       (else_try),
         (ge,"$auto_enter_town",1),
         (start_encounter, "$auto_enter_town"),
       (else_try),
         (ge,"$auto_besiege_town",1),
         (start_encounter, "$auto_besiege_town"),
       (else_try),
         (ge,"$g_camp_mode", 1),
         (assign, "$g_camp_mode", 0),
         (party_set_slot,'p_main_party',slot_party_icon_state,pis_normal),
         (display_message, "@Breaking camp..."),
       (try_end),
    ]),
# Make parties larger as game progresses.
(24,
	[
		(call_script, "script_update_party_creation_random_limits"),
		]
	),
# Updating player icon in every frame
  (0,
   [(troop_get_inventory_slot, ":cur_horse", "trp_player", 8), #horse slot
	(party_get_slot,':cur_icon_state','p_main_party',slot_party_icon_state),
	(party_get_slot,':cur_icon','p_main_party',slot_party_icon),
    (assign, ":new_icon", -1),
    (try_begin),
      (eq, ":cur_icon_state", pis_normal),
      (try_begin),
        (ge, ":cur_horse", 0),
        (assign, ":new_icon", "icon_player_horseman"),
      (else_try),
        (assign, ":new_icon", "icon_player"),
      (try_end),
    (else_try),
      (eq, ":cur_icon_state", pis_camping),
      (assign, ":new_icon", "icon_camp"),
    (try_end),
    (neq, ":new_icon", ":cur_icon"),
    (party_set_slot,'p_main_party',slot_party_icon, ":new_icon"),
    (party_set_icon, "p_main_party", ":new_icon"),
    ]),
# Consuming food at every 8 hours
(8,
	[#(store_sub, ":num_food", food_end, food_begin),
		(party_get_num_companion_stacks, ":num_stacks","p_main_party"),
		(assign, ":num_men", 0),
		(try_for_range, ":i_stack", 0, ":num_stacks"),
			(party_stack_get_size, ":stack_size","p_main_party",":i_stack"),
			(val_add, ":num_men", ":stack_size"),
		(try_end),
		(val_div, ":num_men", 3),
		(try_begin),
			(eq, ":num_men", 0),
			(val_add, ":num_men", 1),
		(try_end),
    
		(assign, ":consumption_amount", ":num_men"),
		(assign, ":no_food_displayed", 0),
		(try_for_range, ":unused", 0, ":consumption_amount"),
			(assign, ":available_food", 0),
			(try_for_range, ":cur_food", rice_goods_begin, food_end),
				(item_set_slot, ":cur_food", slot_item_is_checked, 0),
				(player_has_item, ":cur_food"),
				(val_add, ":available_food", 1),
			(try_end),
			(try_begin),
				(gt, ":available_food", 0),
				(store_random_in_range, ":selected_food", 0, ":available_food"),
				(call_script, "script_consume_food", ":selected_food"),
			(else_try),
				(eq, ":no_food_displayed", 0),
				(display_message, "@Party has nothing to eat!", 0xFF0000),
				(assign, ":no_food_displayed", 1),
			(try_end),
			(assign, ":available_morale_boosters", 0),
			(try_for_range, ":cur_food", food_end, morale_boosters_end),
				(item_set_slot, ":cur_food", slot_item_is_checked, 0),
				(player_has_item, ":cur_food"),
				(val_add, ":available_morale_boosters", 1),
			(try_end),
			(try_begin),
				(gt, ":available_morale_boosters", 0),
				(store_add, ":random_end", ":available_food", ":available_morale_boosters"),
				(store_random_in_range, ":selected_food", ":available_food", ":random_end"),
				(call_script, "script_consume_food", ":selected_food"),
			(try_end),
		(try_end),
		]
	),
# forts
(3,
	[
		(call_script,'script_fort'),
		]
	),
#Player raiding a village
# This trigger will check if player's raid has been completed and will lead control to village menu.
(1,
	[
		(ge,"$g_player_raiding_village",1),
		(try_begin),
			(neq, "$g_player_is_captive", 0),
			(rest_for_hours, 0, 0, 0), #stop resting - abort
			(assign,"$g_player_raiding_village",0),
		(else_try),
			(map_free), #we have been attacked during raid
		(else_try),
			(this_or_next|party_slot_eq, "$g_player_raiding_village", slot_town_state, sts_looted),
			(party_slot_eq, "$g_player_raiding_village", slot_town_state, sts_deserted),
			(start_encounter, "$g_player_raiding_village"),
			(rest_for_hours, 0),
			(assign,"$g_player_raiding_village",0),
			(assign,"$g_player_raid_complete",1),
		(else_try),
			(party_slot_eq, "$g_player_raiding_village", slot_town_state, sts_being_raided),
			(rest_for_hours, 3, 10, 1), #rest while attackable
		(else_try),
			(rest_for_hours, 0, 0, 0), #stop resting - abort
			(assign,"$g_player_raiding_village",0),
			(assign,"$g_player_raid_complete",0),
		(try_end),
    ]),
# Process village raids
(2,
	[
		(call_script, "script_process_raids"),
		]
	),
# check for active patrols
(0,
	[
		(call_script,"script_check_for_active_patrols"),
		],
	),
# adjust weapon durability
(4, # 0,
	[
		#(display_message,'@Item durability script disabled',lt_green_text),
		(eq,1,0),
		(try_for_range,':inv_slot_number',0,4),
			(troop_get_inventory_slot,':item_in_slot','trp_player',':inv_slot_number'),
			(troop_get_inventory_slot_modifier,':item_in_slot_mod','trp_player',':inv_slot_number'),
			(gt,':item_in_slot','itm_no_item'),
			(item_get_slot,':item_dur',':item_in_slot',slot_item_durability),
			(eq,':item_dur',0),
			(str_store_item_name,1,':item_in_slot'),
			(display_message,'@Your {s1} has degraded in quality from overuse.',lt_blue_text),
			(try_begin),
				(this_or_next|is_between,':item_in_slot',metal_weapons_begin,metal_weapons_end),
				(is_between,':item_in_slot',low_quality_weapons_begin,low_quality_weapons_end),
				(try_begin),
					(this_or_next|eq,':item_in_slot_mod',imod_watered_steel),
					(this_or_next|eq,':item_in_slot_mod',imod_heavy),
					(this_or_next|eq,':item_in_slot_mod',imod_balanced),
					(eq,':item_in_slot_mod',imod_plain),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_chipped),
				(else_try),
					(eq,':item_in_slot_mod',imod_chipped),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_rusty),
				(else_try),
					(eq,':item_in_slot_mod',imod_rusty),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_old),
				(try_end),
			(else_try),
				(is_between,':item_in_slot',wooden_weapons_begin,wooden_weapons_end),
				(try_begin),
					(this_or_next|eq,':item_in_slot_mod',imod_heavy),
					(eq,':item_in_slot_mod',imod_plain),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_bent),
				(else_try),
					(eq,':item_in_slot_mod',imod_bent),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_cracked),
				(else_try),
					(eq,':item_in_slot_mod',imod_cracked),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_old),
				(try_end),
			(else_try),
				(is_between,':item_in_slot',ranged_weapons_begin,ranged_weapons_end),
				(try_begin),
					(eq,':item_in_slot_mod',imod_plain),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_bent),
				(else_try),
					(eq,':item_in_slot_mod',imod_bent),
					(troop_set_inventory_slot_modifier,'trp_player',':inv_slot_number',imod_cracked),
				(try_end),
			(try_end),
			(item_set_slot,':item_in_slot',slot_item_durability,20),
		(try_end),
		],
	),
# main economy loop
# city demands are calculated the day after a population check, merchants are sent out every other day. production check happens twice a week. 
# population growth calculation happens once a week
(daily,
	[
		(try_begin),
			(eq,'$g_day_counter',1),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',2),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check and caravan running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',tcs_opt_deliver_goods),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',3),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand and facility production check running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_town_check_facility_production',':town'),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',4),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check and caravan running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',0),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',5),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',tcs_opt_deliver_goods),
				# (call_script,'script_process_unrest',':town'),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',6),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check, production, and caravan running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',0),
				(call_script,'script_town_check_facility_production',':town'),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',7),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',tcs_opt_deliver_goods),
				# (call_script,'script_town_calculate_population',':town'),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',8),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check and caravan running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',0),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',9),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand and facility production check running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_town_check_facility_production',':town'),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',10),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check and caravan running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',0),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',11),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',tcs_opt_deliver_goods),
				# (call_script,'script_process_unrest',':town'),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',12),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check and caravan running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',0),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',13),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand and facility production check running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_town_check_facility_production',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',tcs_opt_deliver_goods),
			(try_end),
		(else_try),
			(eq,'$g_day_counter',14),
			(assign,reg10,'$g_day_counter'),
			(display_message,'@Economy day counter: {reg10}. Demand check and caravan running',debug_text),
			(try_for_range,':town',cities_begin,villages_end),
				(call_script,'script_town_check_supply_and_demand',':town'),
				(call_script,'script_cf_town_generate_caravan',':town',0),
				# (call_script,'script_town_calculate_population',':town'),
			(try_end),
		(try_end),
		# update day counter
		(val_add,'$g_day_counter',1),
		(try_begin),
			(eq,'$g_day_counter',15),
			(assign,'$g_day_counter',1),
		(try_end),
		]
	),
# trading caravan behavior loop
(3,
	[
		(try_for_parties,':caravan'),
			(call_script,'script_cf_party_is_of_template',':caravan','pt_trade_caravan'),
			(call_script,'script_trade_caravan_behavior',':caravan'),
		(try_end),
		]
	),
# create and intitialize new shoen
(weekly,
	[
		# (display_message,'@Shoen initialization disabled',debug_text),
		# (eq,1,0),
		(call_script,'script_cf_get_shoen_candidate',0),
		(assign,':town',reg0),
		(party_get_slot,':capital',':town',slot_town_capital),
		(party_get_slot,':broker',':capital',slot_town_broker),
		(assign,reg10,':broker'),
		(display_message,'@Shoen init: broker is {reg10}',debug_text),
		(neq,':broker','trp_player'),
		(call_script,'script_initialize_shoen',':town',':broker'),
		(str_store_party_name,10,':town'),
		(str_store_party_name,11,':capital'),
		(str_store_troop_name,12,':broker'),
		(display_message,'@The land around {s10} has been consolidated into a shoen. See {s12} in {s11} for more information.',green_text),
		]
	),
# faction ai
(72,
	[
		(call_script,'script_faction_ai_decisor','fac_hosokawa'),
		(call_script,'script_faction_ai_mission','fac_hosokawa',reg0),
		(call_script,'script_faction_ai_decisor','fac_yamana'),
		(call_script,'script_faction_ai_mission','fac_yamana',reg0),
		]
	),
# spy behaviors
(1,
	[
		#(display_message,'@Spy behavior script running...',blue_text),
		(try_for_parties,':spy'),
			(call_script,'script_cf_party_is_of_template',':spy','pt_spy'),
			(party_get_slot,':dest',':spy',slot_spawn_dest),
			(party_get_slot,':origin',':spy',slot_spawn_origin),
			(try_begin),
				(party_is_in_town,':spy',':origin'),
				(store_faction_of_party,':faction',':spy'),
				(try_begin),
					(eq,':faction','fac_hosokawa'),
					(assign,':enemy_fac','fac_yamana'),
				(else_try),
					(assign,':enemy_fac','fac_hosokawa'),
				(try_end),
				(faction_get_slot,':faction_strength',':enemy_fac',slot_faction_strength),
				(store_current_day,':current_day'),
				(val_mul,':current_day',10000),
				(store_add,':score',':current_day',':faction_strength'), # pack the day number and the score together
				(faction_set_slot,':faction',slot_faction_intel,':score'),
				(assign,reg10,':score'),
				(display_message,'@Intelligence mission successful! Intel score: {reg10}',debug_text),
				(remove_party,':spy'),
			(else_try),
				(party_slot_eq,':spy',slot_spawn_delay,-1),
				(party_is_in_town,':spy',':dest'),
				(store_current_day,':current_day'),
				(store_random_in_range,':days_to_wait',0,3),
				(store_add,':delay_for_days',':current_day',':days_to_wait'),
				(party_set_slot,':spy',slot_spawn_delay,':delay_for_days'),
				(assign,reg10,':delay_for_days'),
				(display_message,'@Intelligence mission proceeding! Delay: {reg10}',debug_text),
			(else_try),
				(party_slot_neq,':spy',slot_spawn_delay,-1),
				(store_current_day,':current_day'),
				(party_slot_eq,':spy',slot_spawn_delay,':current_day'),
				(store_random_in_range,':chance_to_survive',0,4),
				(try_begin),
					(eq,':chance_to_survive',0), # success!
					(party_set_ai_behavior,':spy',ai_bhvr_travel_to_party),
					(party_set_ai_object,':spy',':origin'),
					(party_set_slot,':spy',slot_spawn_delay,-1),
					(display_message,'@Intelligence mission proceeding! Running away',debug_text),
				(else_try),
					(remove_party,':spy'), # mission failed, spies were killed
					(display_message,'@Intelligence mission failed!',debug_text),
				(try_end),
			(try_end),
		(try_end),
		]
		),
#Patrol behaviors
(0.5,
	[
		(try_for_parties,':local_patrol'),
			(call_script,'script_cf_party_is_of_template',':local_patrol','pt_local_patrol'),
			(call_script,'script_local_patrol_behavior',':local_patrol'),
		(try_end),
		]),
		
# builders
(1,
	[
		(call_script,'script_builders'),
		]
	),
(0.5,
	[
		(try_for_parties,':fort'),
			(party_get_template_id,':template',':fort'),
			(eq,':template','pt_fort'),
			(party_slot_eq,':fort',slot_castle_patrol_active,1),
			(call_script,'script_cf_fort_patrol_behavior',':fort'),
		(try_end),
		]
	),		
# update reinforcement costs, pay out shoen dividends, disburse trading post revenues
(weekly,
	[
		(call_script,'script_update_reinforcement_spawn_cost'),
		(try_for_parties,':shoen'),
			(call_script,'script_cf_party_is_of_template',':shoen','pt_shoen'),
			(call_script,'script_shoen_pay_dividends',':shoen'),
		(try_end),
		(assign,':tp_disbursement',0),
		(try_for_parties,':trading_post'),
			(call_script,'script_cf_party_is_of_template',':shoen','pt_trading_post'),
			(call_script,'script_trading_post_disburse_revenue',':trading_post'),
			(val_add,':tp_disbursement',reg0),
		(try_end),
		(assign,reg10,':tp_disbursement'),
		(display_message,'@You have earned {reg10} mon from your various trading posts',green_text),
		(troop_add_gold,'trp_player',':tp_disbursement'),
		]
	),
# process sieges
(daily,
	[
		(call_script, "script_process_sieges"),
		]
	),
# main game loop
(1,
	[
		(store_time_of_day,':cur_time'),
		(try_begin),
			(eq,':cur_time',0),
			# make sure $num_active_farmers and $num_active_merchants jive with what is actually active
			(call_script,'script_update_active_merchants'),
			#update town attitudes
			(try_for_range,':input_town',all_towns_begin,all_towns_end),
	            (call_script,"script_modify_town_attitude",':input_town'),
	        (try_end),
			(try_for_range,':town',cities_begin,villages_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_food_stores,0),
					(party_set_slot,':town',slot_town_food_stores,0),
				(try_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_spawn_points,0),
					(party_set_slot,':town',slot_town_spawn_points,0),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',1),
			# modify faction attitudes
			(try_for_range,':input_faction',"fac_neutral","fac_end_fac"),
				(call_script,"script_modify_faction_attitude",':input_faction'),
			(try_end),
			# spawn town patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',towns_begin,towns_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_town_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',2),
			# city patrol spawning
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',hyogo,cities_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_city_patrol'),
				(try_end),
			(try_end),
			(try_for_parties,':patrol'),
				(call_script,'script_cf_party_is_of_template',':patrol','pt_hosokawa_patrol'),
				(call_script,'script_faction_patrol_behavior',':patrol'),
			(try_end),
			(try_for_parties,':patrol'),
				(call_script,'script_cf_party_is_of_template',':patrol','pt_yamana_patrol'),
				(call_script,'script_faction_patrol_behavior',':patrol'),
			(try_end),	
		(else_try),
			(eq,':cur_time',3),
			# spawn village patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',villages_begin,villages_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_village_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',4),
			# update town and temple garrisons
			(try_for_range,':town',cities_begin,villages_end),
				(neg|party_slot_eq,':town',slot_town_under_siege,1),
				(call_script,'script_update_garrison',':town'),
			(try_end),
			(party_get_num_companions,':num_troops','p_temple_1_mercs'),
			(try_begin),
				(lt,':num_troops',15),
				(store_random_in_range,':troops_to_add',1,4),
				(party_add_members,'p_temple_1','trp_monk_t',':troops_to_add'),
				(party_add_members,'p_temple_1_mercs','trp_monk_t',':troops_to_add'),
			(try_end),
			# army behaviors
			(try_for_parties,':army'),
				(call_script,'script_cf_party_is_of_template',':army','pt_army'),
				(call_script,'script_army_behavior',':army'),
			(try_end),
		(else_try),
			(eq,':cur_time',5),
			# reset kokujin registers
			(try_for_range,reg1,"trp_city_5_kokujin","trp_city_1_innkeeper"),
				(store_partner_quest,':active_kokujin_quest'),
				(try_begin),
					(ge,':active_kokujin_quest',0),
					(neg|check_quest_active,':active_kokujin_quest'),
					(troop_set_slot,reg1,slot_troop_quest_active,0),
				(else_try),
					(troop_set_slot,reg1,slot_troop_quest_active,0),
				(try_end),
			(try_end),
			(troop_get_slot,reg2,0,slot_player_lord),
			(try_begin),
				(gt,reg2,0),
				(assign,':flag',0),
				(try_for_range,':quest','qst_kuge_emissary','qst_end_quests'),
					 (check_quest_active,':quest'),
					 (assign,':flag',1),
				(try_end),
				(try_begin),
					 (eq,':flag',0),
					 (troop_set_slot,reg2,slot_troop_quest_active,0),
				(try_end),
			(try_end),
			# update innkeeper attitudes 
			(try_for_range,':input_troop',innkeepers_begin,innkeepers_end),
				(call_script,"script_modify_troop_attitude",':input_troop'),
			(try_end),
		(else_try),
			(eq,':cur_time',6),
			(call_script,'script_refresh_garrison_source_parties'),
			(try_for_range,':town',cities_begin,villages_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_food_stores,0),
					(party_set_slot,':town',slot_town_food_stores,0),
				(try_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_spawn_points,0),
					(party_set_slot,':town',slot_town_spawn_points,0),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',7),
			# modify faction attitudes
			(try_for_range,':input_faction',"fac_neutral","fac_end_fac"),
				(call_script,"script_modify_faction_attitude",':input_faction'),
			(try_end),
			# spawn village patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',villages_begin,villages_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_village_patrol'),
				(try_end),
			(try_end),
			# spawn town patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',towns_begin,towns_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_town_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',8),
			#(display_message,'@Current time is {reg1}'),
			# Party morale
			(try_begin),
				(val_mul, "$g_party_morale_change_due_to_battles", 4),
				(val_div, "$g_party_morale_change_due_to_battles", 5),
				(call_script, "script_process_player_party_morale"),
			(try_end),
		(else_try),
			(eq,':cur_time',9),
			#(display_message,'@Current time is {reg1}'),
			(try_for_parties,':shoen'),
				(call_script,'script_cf_party_is_of_template',':shoen','pt_shoen'),
				(call_script,'script_shoen_update_production',':shoen'),
				(call_script,'script_shoen_update_moving_averages',':shoen'),
			(try_end),
		(else_try),
			(eq,':cur_time',10),
			#(display_message,'@Current time is {reg1}'),
			# city patrol spawning
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',hyogo,cities_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_city_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',11),
			# spawn village patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',villages_begin,villages_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_village_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',12),
			# Daily rice consumption
			(try_begin),
				#(display_message,'@Rice consumption is disabled',lt_blue_text),
				(eq,1,0),
				(try_for_parties,':party'),
					(neq,':party',player_party),
					(call_script,'script_rice_consumption',':party'),
				(try_end),
			(try_end),
			# (try_for_range,':npc_lord',npc_lords_begin,npc_lords_end),
				# (store_random_in_range,reg10,0,4),
				# (eq,reg10,0),
				# (call_script,'script_cf_shoen_ai_buy_shares',':npc_lord'),
			# (try_end),
			# (try_for_range,':npc_lord',npc_lords_begin,npc_lords_end),
				# (store_random_in_range,reg10,0,3),
				# (eq,reg10,0),
				# (call_script,'script_cf_shoen_ai_sell_shares','trp_hosokawa_katsumoto'),
			# (try_end),
			(try_for_range,':town',cities_begin,villages_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_food_stores,0),
					(party_set_slot,':town',slot_town_food_stores,0),
				(try_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_spawn_points,0),
					(party_set_slot,':town',slot_town_spawn_points,0),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',13),
			# modify faction attitudes
			(try_for_range,':input_faction',"fac_neutral","fac_end_fac"),
				(call_script,"script_modify_faction_attitude",':input_faction'),
			(try_end),
			# spawn town patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',towns_begin,towns_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_town_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',14),
			(try_for_parties,':army'),
				(call_script,'script_cf_party_is_of_template',':army','pt_army'),
				(call_script,'script_army_behavior',':army'),
			(try_end),
			(try_for_parties,':patrol'),
				(call_script,'script_cf_party_is_of_template',':patrol','pt_hosokawa_patrol'),
				(call_script,'script_faction_patrol_behavior',':patrol'),
			(try_end),
			(try_for_parties,':patrol'),
				(call_script,'script_cf_party_is_of_template',':patrol','pt_yamana_patrol'),
				(call_script,'script_faction_patrol_behavior',':patrol'),
			(try_end),
		(else_try),
			(eq,':cur_time',15),
			# spawn village patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',villages_begin,villages_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_village_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',16),
			# update weaponsmith, armorer, goods merchant inventory
			(try_begin),
				(try_for_range,':town',cities_begin,towns_end),
					(party_get_slot,':weaponsmith',':town',slot_town_weaponsmith),
					(reset_item_probabilities,100),
					(store_random_in_range,':quality',90,111),
					(set_merchandise_modifier_quality,':quality'),
					(troop_add_merchandise,':weaponsmith',itp_type_one_handed_wpn,5),
					(troop_add_merchandise,':weaponsmith',itp_type_two_handed_wpn,5),
					(troop_add_merchandise,':weaponsmith',itp_type_polearm,5),
					(troop_add_merchandise,':weaponsmith',itp_type_bow,4),
					(troop_add_merchandise,':weaponsmith',itp_type_arrows,2),
					(troop_add_merchandise,':weaponsmith',itp_type_thrown,2),
					(troop_ensure_inventory_space,':weaponsmith',merchant_inventory_space),
					(store_troop_gold,':merchant_gold',':weaponsmith'),
					(lt,':merchant_gold',15000),
					(store_random_in_range,':new_merchant_gold',360,3600),
					(val_add,':new_merchant_gold',360),
					(troop_add_gold,':weaponsmith',':new_merchant_gold'),
					# armorer
					(party_get_slot,':armorer',':town',slot_town_armorer),
					(reset_item_probabilities,100),
					(store_random_in_range,':quality',90,111),
					(set_merchandise_modifier_quality,':quality'),
					(troop_add_merchandise,':armorer',itp_type_body_armor,12),
					(troop_add_merchandise,':armorer',itp_type_head_armor,12),
					(troop_add_merchandise,':armorer',itp_type_foot_armor,8),
					(troop_add_merchandise,':armorer',itp_type_hand_armor,4),
					(troop_ensure_inventory_space,':armorer',merchant_inventory_space),
					(store_troop_gold,':merchant_gold',':armorer'),
					(lt,':merchant_gold',45000),
					(store_random_in_range,':new_merchant_gold',3600,14400),
					(val_add,':new_merchant_gold',1800),
					(troop_add_gold,':armorer',':new_merchant_gold'),
				(try_end),
				(try_for_range,':town',cities_begin,villages_end),
					(party_get_slot,':goods_merchant',':town',slot_town_merchant),
					(reset_item_probabilities,100),
					(store_random_in_range,reg1,90,111),
					(set_merchandise_modifier_quality,reg1),
					(party_get_slot,':trade_good_abundance_array',':town',slot_town_trade_good_abundance_array),
					(assign,':array_index',0),
					(try_for_range,":item",trade_goods_begin,trade_goods_end),
						(call_script,'script_array_peek',':trade_good_abundance_array',':array_index'),
						(set_item_probability_in_merchandise,":item",'$return'),
						(val_add,':array_index',1),
					(try_end),
					(troop_add_merchandise,":goods_merchant",itp_type_goods,num_merchandise_goods),
					(call_script,'script_troop_remove_items',':goods_merchant',food_begin,food_end),
					(troop_add_merchandise,':goods_merchant',itp_type_horse,12),
					(troop_ensure_inventory_space,":goods_merchant",merchant_inventory_space),
					(store_troop_gold, ":merchant_gold",":goods_merchant"),
					(try_begin),
						(lt,":merchant_gold",20000),
						(store_random_in_range,":new_merchant_gold",1000,3000),
						(troop_add_gold,":goods_merchant",":new_merchant_gold"),
					(try_end),
					# update fishmonger inventory
					(party_get_slot,':fishmonger',':town',slot_town_fishmonger),
					(call_script,'script_troop_add_merchandise',':town',':fishmonger',food_begin,food_end,15,30,0),
					(store_troop_gold, ":merchant_gold",":fishmonger"),
					(try_begin),
						(lt,":merchant_gold",5000),
						(store_random_in_range,":new_merchant_gold",500,1000),
						(troop_add_gold,":fishmonger",":new_merchant_gold"),
					(try_end),
					(troop_ensure_inventory_space,":fishmonger",merchant_inventory_space),
					# update sake merchant/broker inventory
					(try_begin),
						(party_slot_neq,':town',slot_town_broker,null),
						(party_get_slot,':broker',':town',slot_town_broker),
						(call_script,'script_troop_add_merchandise',':town',':broker',rice_goods_begin,rice_goods_end,20,30,100),
						(store_troop_gold, ":merchant_gold",":broker"),
						(try_begin),
							(lt,":merchant_gold",50000),
							(store_random_in_range,":new_merchant_gold",5000,10000),
							(troop_add_gold,":broker",":new_merchant_gold"),
						(try_end),
						(troop_ensure_inventory_space,":broker",merchant_inventory_space),
					(try_end),
				(try_end),
			(try_end),
			(try_for_parties,':shoen'),
				(call_script,'script_cf_party_is_of_template',':shoen','pt_shoen'),
				(call_script,'script_shoen_set_dividend',':shoen'),
			(try_end),
		(else_try),
			(eq,':cur_time',17),
			# update innkeeper attitudes 
			(try_for_range,':input_troop',innkeepers_begin,innkeepers_end),
				(call_script,"script_modify_troop_attitude",':input_troop'),
			(try_end),
			# update trade good prices and abundance
			(try_for_range,':town',cities_begin,villages_end),
				(party_get_slot,':trade_good_abundance_array',':town',slot_town_trade_good_abundance_array),
				(party_get_slot,':trade_good_price_array',':town',slot_town_trade_good_price_array),
				(party_get_slot,':aggregate_demand',':town',slot_town_demand),
				(store_add,':price_multiplier',100,':aggregate_demand'),
				(assign,':array_index',0),
				(try_for_range,':item',trade_goods_begin,trade_goods_end),
					(call_script,'script_calculate_abundance',':town',':item'),
					(call_script,'script_array_poke',':trade_good_abundance_array',':array_index',reg0),
					(store_item_value,':item_value',':item'),
					(val_mul,':item_value',':price_multiplier'),
					(val_div,':item_value',100),
					# add other price manipulations here
					(call_script,'script_array_poke',':trade_good_price_array',':array_index',':item_value'),
					(val_add,':array_index',1),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',18),
			#(display_message,'@Current time is {reg1}'),
			(call_script,'script_refresh_garrison_source_parties'),
			(try_for_range,':town',cities_begin,villages_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_food_stores,0),
					(party_set_slot,':town',slot_town_food_stores,0),
				(try_end),
				(try_begin),
					(party_slot_lt,':town',slot_town_spawn_points,0),
					(party_set_slot,':town',slot_town_spawn_points,0),
				(try_end),
			(try_end),
			# city patrol spawning
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',hyogo,cities_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_city_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',19),
			# modify faction attitudes
			(try_for_range,':input_faction',"fac_neutral","fac_end_fac"),
				(call_script,"script_modify_faction_attitude",':input_faction'),
			(try_end),
			# spawn village patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',villages_begin,villages_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_village_patrol'),
				(try_end),
			(try_end),
			# spawn town patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',towns_begin,towns_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_town_patrol'),
				(try_end),
			(try_end),
		(else_try),
			(eq,':cur_time',20),
			(try_for_parties,':army'),
				(call_script,'script_cf_party_is_of_template',':army','pt_army'),
				(call_script,'script_army_behavior',':army'),
			(try_end),
		(else_try),
			(eq,':cur_time',21),
			# generate base town revenue
			(try_for_range,':town',all_towns_begin,all_towns_end),
				(party_slot_neq,':town',slot_town_under_siege,1),
				(call_script,'script_generate_base_revenue',':town'),
			(try_end),
		(else_try),
			(eq,':cur_time',22),
			# calculate faction spawn points
			(call_script,'script_calculate_faction_spawn_points','fac_hosokawa'),
			(call_script,'script_calculate_faction_spawn_points','fac_yamana'),
			(call_script,'script_calculate_faction_spawn_points','fac_outlaws'),
			(call_script,'script_check_city_status','fac_hosokawa'),
			(call_script,'script_check_city_status','fac_yamana'),
		(else_try),
			(eq,':cur_time',23),
			# spawn village patrols
			(try_begin),
				(store_num_parties_of_template,':num_patrols','pt_local_patrol'),
				(le,':num_patrols','$max_local_patrols'),
				(try_for_range,':town',villages_begin,villages_end),
					(party_slot_neq,':town',slot_town_patrol_active,1),
					(call_script,'script_cf_spawn_local_patrol',':town','pt_village_patrol'),
				(try_end),
			(try_end),
			# update faction strength
			(call_script,'script_faction_strength','fac_hosokawa',0),
			(call_script,'script_faction_strength','fac_yamana',0),
		(try_end),
		(try_for_parties,':trading_post'),
			(call_script,'script_cf_party_is_of_template',':trading_post','pt_trading_post'),
			(call_script,'script_process_trading_posts',':trading_post'),
		(try_end),
		]
	),
# update patrol attitudes
(36,
	[
		(try_for_parties,':patrol'),
			(party_get_template_id,':template',':patrol'),
			(try_begin),
				(this_or_next|eq,':template','pt_local_patrol'),
				(this_or_next|eq,':template','pt_hosokawa_allied_patrol'),
				(this_or_next|eq,':template','pt_yamana_allied_patrol'),
				(this_or_next|eq,':template','pt_hosokawa_patrol'),
				(eq,':template','pt_yamana_patrol'),
				(party_get_slot,':origin',':patrol',slot_spawn_origin),
				(call_script,"script_update_patrol_attitude",':patrol',':origin'),
			(try_end),
		(try_end),
		]
	),
(weekly,
	[
		#(display_message,'@Rice production is running',pink_text),
		(try_for_range,':town',cities_begin,villages_end),
			(call_script,'script_rice_production',':town'),
		(try_end),
		]
	),
#Pay day.
(weekly,
	[
		(jump_to_menu,"mnu_pay_day"),
		]
	),
# army_upkeep
(weekly,
	[
		(jump_to_menu,'mnu_army_upkeep'),
		],
	),
# refresh garrison source parties
# update farmer mercs
(daily,
	[
		# Refresh Hosokawa/Yamana mercs
		(try_begin),
			(party_get_num_companions,reg2,"p_hosokawa_mercs"),
			(le,reg2,10),
			(party_add_members,"p_hosokawa_mercs","trp_t2_spear_ashigaru_h",10),
		(try_end),
		(try_begin),
			(party_get_num_companions,reg2,"p_yamana_mercs"),
			(le,reg2,10),
			(party_add_members,"p_yamana_mercs","trp_t2_spear_ashigaru_y",10),
		(try_end),
   # Refresh farmer troops
		(try_for_range,':merc_source',"p_city_1_mercs","p_town_1_mercs"),
			(party_count_companions_of_type,reg4,':merc_source',"trp_farmer"),
			(lt,reg4,"$max_city_farmers"),
			(store_random_in_range,reg5,1,4),
			(party_add_members,':merc_source',"trp_farmer",reg5),
		(try_end),
		(try_for_range,':merc_source',"p_town_1_mercs","p_town_6_mercs"),
			(party_count_companions_of_type,reg4,':merc_source',"trp_farmer"),
			(lt,reg4,"$max_large_town_farmers"),
			(store_random_in_range,reg5,1,4),
			(party_add_members,':merc_source',"trp_farmer",reg5),
		(try_end),
		(try_for_range,':merc_source',"p_town_6_mercs","p_village_1_mercs"),
			(party_count_companions_of_type,reg4,':merc_source',"trp_farmer"),
			(lt,reg4,"$max_town_farmers"),
			(store_random_in_range,reg5,1,4),
			(party_add_members,':merc_source',"trp_farmer",reg5),
		(try_end),
		(try_for_range,':town',villages_begin,villages_end),
			(party_get_slot,':town_size',':town',slot_town_size),
			(eq,':town_size',ct_large_village),
			(party_get_slot,':merc_source',':town',slot_town_mercs),
			(party_count_companions_of_type,':troop_count',':merc_source',"trp_farmer"),
			(lt,':troop_count',"$max_large_village_farmers"),
			(store_random_in_range,reg5,1,4),
			(party_add_members,':merc_source',"trp_farmer",reg5),
		(try_end),
		(try_for_range,':town',villages_begin,villages_end),
			(try_begin),
				(neq,':town',iga),
				(party_get_slot,':town_size',':town',slot_town_size),
				(this_or_next|eq,':town_size',ct_village),
				(eq,':town_size',ct_small_village),
				(party_get_slot,':merc_source',':town',slot_town_mercs),
				(party_count_companions_of_type,':troop_count',':merc_source',"trp_farmer"),
				(lt,':troop_count',"$max_village_farmers"),
				(store_random_in_range,reg5,1,4),
				(party_add_members,':merc_source',"trp_farmer",reg5),
			(else_try),
				(eq,':town',iga),
				(party_get_slot,':merc_source',':town',slot_town_mercs),
				(party_count_companions_of_type,':troop_count',"trp_shinobi_apprentice",':merc_source'),
				(lt,':troop_count',"$max_village_farmers"),
				(store_random_in_range,reg5,1,4),
				(party_add_members,':merc_source',"trp_shinobi_apprentice",reg5),
			(try_end),
		(try_end),
		]
	),
# refresh ronin mercs
(weekly,
	[
		(try_for_range,':merc_source',"p_city_1_mercs","p_village_1_mercs"),
			(try_begin),
				(is_between,':merc_source',"p_city_1_mercs","p_town_1_mercs"),
				(party_count_companions_of_type,':num_ronin',':merc_source',"trp_ronin"),
				(le,':num_ronin',"$max_city_ronin"),
				(store_random_in_range,reg4,1,4),
				(party_add_members,':merc_source',"trp_ronin",reg4),
			(else_try),
				(is_between,':merc_source',"p_town_1_mercs","p_town_6_mercs"),
				(party_count_companions_of_type,':num_ronin',':merc_source',"trp_ronin"),
				(le,':num_ronin',"$max_large_town_ronin"),
				(store_random_in_range,reg4,1,4),
				(party_add_members,':merc_source',"trp_ronin",reg4),
			(else_try),
				(party_count_companions_of_type,':num_ronin',':merc_source',"trp_ronin"),
				(le,':num_ronin',"$max_town_ronin"),
				(store_random_in_range,reg4,1,4),
				(party_add_members,':merc_source',"trp_ronin",reg4),
			(try_end),
		(try_end),
		(try_begin),
			(party_get_num_companions,':num_disciples','p_hyogo_disciples'),
			(lt,':num_disciples',10),
			(try_begin),
				(check_quest_active,'qst_faction_hosokawa'),
				(assign,':disciple_troop','trp_disciple_h'),
			(else_try),
				(check_quest_active,'qst_faction_yamana'),
				(assign,':disciple_troop','trp_disciple_y'),
			(else_try),
				(assign,':disciple_troop','trp_disciple'),
			(try_end),
			(store_random_in_range,reg1,0,4),
			(party_add_members,'p_hyogo_disciples',':disciple_troop',reg1),
		(try_end),
		]
	),
# semi weekly demand trigger
(84,
	[
		#(display_message,'@Demand script running...',green_text),
		(call_script,'script_demand'),
		]
	),
# clock trigger
#(1,
#	[
#		(val_add,'$time',1),
#		(try_begin),
#			(eq,'$time',13),
#			(assign,'$time',1),
#		(try_end),
#		(try_begin),
#			(eq,'$clock',1),
#			(assign,reg55,'$time'),
			#(display_message,'@{reg55} o'clock'),
#		(try_end),
#		],
#	),
# restore kokujin registers
# spawn bandits
(3,
	[
		(eq,1,0),
		(eq,'$g_bandits_spawn_flag',0),
		(call_script,'script_spawn_bandits'),
		]
	),
#Party AI: pruning some of the prisoners in each center (once a week)
  (24*7,
   [
       (try_for_range, ":center_no", hyogo, villages_end),
         (party_get_num_prisoner_stacks, ":num_prisoner_stacks",":center_no"),
		 (gt,':num_prisoner_stacks',0),
         (try_for_range_backwards, ":stack_no", 0, ":num_prisoner_stacks"),
           (party_prisoner_stack_get_troop_id, ":stack_troop",":center_no",":stack_no"),
           (neg|troop_is_hero, ":stack_troop"),
           (party_prisoner_stack_get_size, ":stack_size",":center_no",":stack_no"),
           (store_random_in_range, ":rand_no", 0, 40),
           (val_mul, ":stack_size", ":rand_no"),
           (val_div, ":stack_size", 100),
           (party_remove_prisoners, ":center_no", ":stack_troop", ":stack_size"),
         (try_end),
       (try_end),
    ]),

]

debug = """
  (1,
   [
      (gt,"$auto_besiege_town",0),
      (gt,"$g_player_besiege_town", 0),
      (ge, "$g_siege_method", 1),
      (store_current_hours, ":cur_hours"),
      (eq, "$g_siege_force_wait", 0),
      (ge, ":cur_hours", "$g_siege_method_finish_hours"),
      (neg|is_currently_night),
      (rest_for_hours, 0, 0, 0), #stop resting
    ]),

  # Banner selection menu
  (24,
   [(eq, "$g_player_banner_granted", 1),
    (troop_slot_eq, "trp_player", slot_troop_banner_scene_prop, 0),
    (le,"$auto_menu",0),
    (start_presentation, "prsnt_banner_selection"),
    ]),
	
  #Adding net incomes to heroes (once a week)
  #Hiring men with hero wealths (once a week)
  #Increasing debts to heroes by 1% (once a week)
  #Adding net incomes to centers (once a week)
  #Hiring men with center wealths (once a week)
  (24*7,
   [
       (try_for_range, ":troop_no", kingdom_heroes_begin, kingdom_heroes_end),
         (troop_get_slot, ":cur_debt", ":troop_no", slot_troop_player_debt),#Increasing debt
         (val_mul, ":cur_debt", 101),
         (val_div, ":cur_debt", 100),
         (troop_set_slot, ":troop_no", slot_troop_player_debt, ":cur_debt"),
         (call_script, "script_calculate_hero_weekly_net_income_and_add_to_wealth", ":troop_no"),#Adding net income
         (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
         (ge, ":party_no", 1),
         (party_get_attached_to, ":cur_attached_party", ":party_no"),
         (is_between, ":cur_attached_party", centers_begin, centers_end),
         (party_get_battle_opponent, ":besieger_party", ":cur_attached_party"),
         (lt, ":besieger_party", 0),
         (call_script, "script_hire_men_to_kingdom_hero_party", ":troop_no"),#Hiring men with current wealth
       (try_end),
       (try_for_range, ":center_no", centers_begin, centers_end),
         (try_begin),
           #If non-player center, adding income to wealth and hiring troops
           (party_get_slot, ":cur_wealth", ":center_no", slot_town_wealth),
           (try_begin),
             (party_slot_eq, ":center_no", slot_party_type, spt_town),
             (val_add, ":cur_wealth", 1000),
           (else_try),
             (val_add, ":cur_wealth", 500),
           (try_end),
           (call_script, "script_calculate_weekly_party_wage", ":center_no"),
           (val_sub, ":cur_wealth", reg0),

           (assign, ":hiring_budget", ":cur_wealth"),
           (val_div, ":hiring_budget", 20),
           (try_begin),
             (gt, ":hiring_budget", reinforcement_cost),
             (neg|party_slot_ge, ":center_no", slot_town_is_besieged_by, 0),
             (call_script, "script_cf_reinforce_party", ":center_no"),
             (val_sub, ":cur_wealth", reinforcement_cost),
           (try_end),
           (party_set_slot, ":center_no", slot_town_wealth, ":cur_wealth"),
         (try_end),
       (try_end),
    ]),

#Troop AI: Kingdom Heroes thinking
##  (24,
##   [
##       (try_for_range, ":troop_no", kingdom_heroes_begin, kingdom_heroes_end),
##         (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
##         (troop_slot_eq, ":troop_no", slot_troop_is_prisoner, 0),
##         (troop_get_slot, ":party_no", ":troop_no", slot_troop_leaded_party),
##         (gt, ":party_no", 0),
##         (party_get_attached_to, ":center_no", ":party_no"),
##         (store_troop_faction, ":faction_no", ":troop_no"),
##
##         (call_script, "script_party_count_fit_for_battle", ":party_no"),
##         (assign, ":party_fit_for_battle", reg0),
##
##         (assign, ":chance_move_to_home_center", 0),
##         (assign, ":target_move_to_home_center", -1),
##         (assign, ":chance_move_to_other_center", 0),
##         (assign, ":target_move_to_other_center", -1),
##         (assign, ":chance_besiege_enemy_center", 0),
##         (assign, ":target_besiege_enemy_center", -1),
##         (assign, ":chance_help_besieged_center", 0),
##         (assign, ":target_help_besieged_center", -1),
##         (assign, ":chance_patrol_around_center", 0),
##         (assign, ":target_patrol_around_center", -1),
##         (assign, ":chance_stay", 0),
##       
##         (assign, ":is_besieging", 0),
##         (assign, ":under_siege", 0),
##         (try_begin),
##           (party_get_battle_opponent, ":enemy_leader_party", ":party_no"),
##           (ge, ":enemy_leader_party", 0),
##           (party_get_battle_opponent, ":leader_party", ":enemy_leader_party"),
##           (is_between, ":leader_party", centers_begin, centers_end),
##           (assign, ":under_siege", 1),
##         (try_end),
##         (assign, ":siege_going_well", 0),
##         (assign, ":siege_going_badly", 0),
##         (try_begin),
##           (party_get_battle_opponent, ":besieged_center", ":party_no"),
##           (is_between, ":besieged_center", centers_begin, centers_end), #we are besieging
##       
##           (party_get_battle_opponent, ":leader_party", ":besieged_center"),
##       
##           (party_collect_attachments_to_party, ":besieged_center", "p_temp_party"),
##           (call_script, "script_party_calculate_strength", "p_temp_party"),
##           (assign, ":besieged_center_strength", reg0),
##           (val_max, ":besieged_center_strength", 1),
##
##           (party_collect_attachments_to_party, ":leader_party", "p_temp_party"),
##           (call_script, "script_party_calculate_strength", "p_temp_party"),
##           (assign, ":besiegers_strength", reg0),
##           (val_max, ":besiegers_strength", 1),
##
##           (try_begin),
##             (gt, ":besiegers_strength", ":besieged_center_strength"),
##             (assign, ":siege_going_well", 1),
##           (else_try),
##             (assign, ":siege_going_badly", 1),
##           (try_end),
##         (try_end),
##
##         (eq, ":under_siege", 0),
##         (try_begin),
##          
##           (assign, ":target_move_to_home_center", -1), #Moving to home center
##           (try_begin),
##             (is_between, ":center_no", centers_begin, centers_end),
##             (party_slot_eq,  ":center_no", slot_town_lord, ":troop_no"),
##             (assign, ":target_move_to_home_center", ":center_no"),
##             (assign, ":chance_move_to_home_center", 100),
##           (try_end),
##           (try_begin),
##             (eq, ":target_move_to_home_center", -1),
##             (call_script, "script_cf_troop_get_random_leaded_center", ":troop_no"),#Can fail
##             (assign, ":target_move_to_home_center", reg0),
##             (assign, ":chance_move_to_home_center", 50),
##           (try_end),
##         (try_end),
##         (try_begin),
##           
##           (assign, ":target_move_to_other_center", -1),#Moving to other center
##           (try_begin),
##             (party_slot_eq, ":party_no", slot_party_ai_state, spai_holding_center),
##             (party_get_slot, ":target_move_to_other_center", ":party_no", slot_party_ai_object),
##             (assign, ":chance_move_to_other_center", 30),
##           (try_end),
##           (try_begin),
##             (eq, ":target_move_to_other_center", -1),
##             (call_script, "script_cf_select_random_center_with_faction", ":faction_no"),
##             (assign, ":target_move_to_other_center", reg0),
##             (assign, ":chance_move_to_other_center", 10),
##             (party_get_slot, ":lord_of_center", ":target_move_to_other_center", slot_town_lord),
##             (try_begin),
##               (call_script, "script_cf_troop_check_troop_is_enemy", ":troop_no", ":lord_of_center"),
##             (else_try),
##               (assign, ":chance_move_to_other_center", 0),
##             (try_end),
##           (try_end),
##         (try_end),
##         (try_begin),
##           
##           (assign, ":target_besiege_enemy_center", -1),#Initiate siege
##           (try_begin),
##             (party_slot_eq, ":party_no", slot_party_ai_state, spai_besieging_center),
##             (party_get_slot, ":target_besiege_enemy_center", ":party_no", slot_party_ai_object),
##           (try_end),
##           (try_begin),
##             (eq, ":target_besiege_enemy_center", -1),
##             (try_begin),
##               (call_script, "script_cf_get_random_siege_location_with_attacker_faction", ":faction_no"),#Can fail
##               (assign, ":target_besiege_enemy_center", reg0),
##               (party_get_battle_opponent, ":target_party", ":target_besiege_enemy_center"),
##               (call_script, "script_cf_get_party_leader", ":target_party"),#Can fail
##               (assign, ":target_lord", reg0),
##               (try_begin),
##                 (call_script, "script_cf_troop_check_troop_is_enemy", ":troop_no", ":target_lord"),#Can fail
##                 (assign, ":target_besiege_enemy_center", -1),
##               (try_end),
##             (try_end),
##             (try_begin),
##               (eq, ":target_besiege_enemy_center", -1),
##               (call_script, "script_get_random_enemy_center", ":party_no"),
##               (assign, ":target_besiege_enemy_center", reg0),
##             (try_end),
##           (try_end),
##           (try_begin),
##             (is_between, ":target_besiege_enemy_center", centers_begin, centers_end),
##             (party_collect_attachments_to_party, ":target_besiege_enemy_center", "p_temp_party"),
##             (call_script, "script_party_calculate_strength", "p_temp_party"),
##             (assign, ":enemy_strength", reg0),
##             (val_max, ":enemy_strength", 1),
##             (party_clear, "p_temp_party"),
##             (try_begin),
##               (party_get_battle_opponent, ":friend_party", ":target_besiege_enemy_center"),
##               (gt, ":friend_party", 0),
##               (party_collect_attachments_to_party, ":friend_party", "p_temp_party"),
##             (try_end),
##             (try_begin),
##               (neq, ":center_no", ":friend_party"),
##               (assign, "$g_move_heroes", 1),
##               (call_script, "script_party_add_party_companions", "p_temp_party", ":party_no"),
##             (try_end),
##             (call_script, "script_party_calculate_strength", "p_temp_party"),
##             (assign, ":our_strength", reg0),
##             (store_mul, ":strength_ratio", ":our_strength", 100),
##             (val_div, ":strength_ratio", ":enemy_strength"),
##       
##             (gt, ":strength_ratio", 90),
##             (store_div, ":chance_besiege_enemy_center", ":strength_ratio", 4),
##             (store_distance_to_party_from_party, ":cur_distance", ":party_no", ":target_besiege_enemy_center"),
##             (val_add, ":cur_distance", 20),
##             (val_mul, ":chance_besiege_enemy_center", 20),
##             (val_div, ":chance_besiege_enemy_center", ":cur_distance"),
##           (try_end),
##         (try_end),
##         (try_begin),
##           
##           (assign, ":target_help_besieged_center", -1),#Help besieged center
##           (try_begin),
##             (party_slot_eq, ":party_no", slot_party_ai_state, spai_helping_town_against_siege),
##             (party_get_slot, ":target_help_besieged_center", ":party_no", slot_party_ai_object),
##           (try_end),
##           (try_begin),
##             (eq, ":target_help_besieged_center", -1),
##             (call_script, "script_cf_get_random_siege_location_with_faction", ":faction_no"),#Can fail
##             (assign, ":target_help_besieged_center", reg0),
##           (try_end),
##           (try_begin),
##             (neq, ":target_help_besieged_center", -1),
##             (party_get_slot, ":target_lord", ":target_help_besieged_center", slot_town_lord),
##             (try_begin),
##               (call_script, "script_cf_troop_check_troop_is_enemy", ":troop_no", ":target_lord"),#Can fail
##             (else_try),
##               (gt, ":party_fit_for_battle", 20),
##               (assign, ":chance_help_besieged_center", 10),
##             (try_end),
##           (try_end),
##         (try_end),
##         (try_begin),
##           
##           (assign, ":target_patrol_around_center", -1),#Patrol
##           (try_begin),
##             (party_slot_eq, ":party_no", slot_party_ai_state, spai_patrolling),
##             (party_get_slot, ":target_patrol_around_center", ":party_no", slot_party_ai_object),
##           (try_end),
##           (try_begin),
##             (eq, ":target_patrol_around_center", -1),
##             (call_script, "script_get_closest_center_of_faction", ":party_no", ":faction_no"),
##             (assign, ":target_patrol_around_center", reg0),
##           (try_end),
##           (try_begin),
##              (neg|faction_slot_eq,":faction_no",slot_faction_leader, ":troop_no"),
##              (gt, ":party_fit_for_battle", 15),
##              (is_between, ":target_patrol_around_center", centers_begin, centers_end),
##              (assign, ":chance_patrol_around_center", 50),
##           (try_end),
##         (try_end),
##         (try_begin),
##           
##           (party_get_slot, ":ai_state", ":party_no", slot_party_ai_state),#Stay
##           (neq, ":ai_state", spai_undefined),
##           (eq, ":is_besieging", 0),
##           (assign, ":chance_stay", 100),
##         (try_end),
##
##         (try_begin),
##           (eq, ":siege_going_badly", 1),
##           (assign, ":chance_besiege_enemy_center", 0),
##           (assign, ":chance_stay", 0),
##         (try_end),
##         (try_begin),
##           (eq, ":siege_going_well", 1),
##           (assign, ":chance_move_to_home_center", 0),
##           (assign, ":chance_move_to_other_center", 0),
##           (assign, ":chance_patrol_around_center", 0),
##           (assign, ":chance_besiege_enemy_center", 0),
##           (assign, ":chance_help_besieged_center", 0),
##         (try_end),
##
##         (assign, ":sum_chances", ":chance_move_to_home_center"),
##         (val_add, ":sum_chances", ":chance_move_to_other_center"),
##         (val_add, ":sum_chances", ":chance_besiege_enemy_center"),
##         (val_add, ":sum_chances", ":chance_help_besieged_center"),
##         (val_add, ":sum_chances", ":chance_patrol_around_center"),
##         (val_add, ":sum_chances", ":chance_stay"),
##         (store_random_in_range, ":random_no", 0, ":sum_chances"),
##         (val_sub, ":random_no", ":chance_move_to_home_center"),
##         (try_begin),
##           (lt, ":random_no", 0),
##           (party_set_ai_object, ":party_no", ":target_move_to_home_center"),
##           (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
##           (party_set_slot, ":party_no", slot_party_ai_state, spai_holding_center),
##           (party_set_slot, ":party_no", slot_party_ai_object, ":target_move_to_home_center"),
##           (try_begin),
##             (is_between, ":center_no", centers_begin, centers_end),
##             (party_detach, ":party_no"),
##           (try_end),
##           (party_set_flags, ":party_no", pf_default_behavior, 1),
##         (else_try),
##           (val_sub, ":random_no", ":chance_move_to_other_center"),
##           (lt, ":random_no", 0),
##           (party_set_ai_object, ":party_no", ":target_move_to_other_center"),
##           (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
##           (party_set_flags, ":party_no", pf_default_behavior, 0),
##           (party_set_slot, ":party_no", slot_party_ai_state, spai_holding_center),
##           (party_set_slot, ":party_no", slot_party_ai_object, ":target_move_to_other_center"),
##           (try_begin),
##             (is_between, ":center_no", centers_begin, centers_end),
##             (party_detach, ":party_no"),
##           (try_end),
##         (else_try),
##           (val_sub, ":random_no", ":chance_besiege_enemy_center"),
##           (lt, ":random_no", 0),
##           (party_set_ai_object, ":party_no", ":target_besiege_enemy_center"),
##           (party_set_ai_behavior, ":party_no", ai_bhvr_attack_party),
##           (party_set_slot, ":party_no", slot_party_ai_state, spai_besieging_center),
##           (party_set_slot, ":party_no", slot_party_ai_object, ":target_besiege_enemy_center"),
##           (try_begin),
##             (is_between, ":center_no", centers_begin, centers_end),
##             (party_detach, ":party_no"),
##           (try_end),
##           (party_set_flags, ":party_no", pf_default_behavior, 1),
##         (else_try),
##           (val_sub, ":random_no", ":chance_help_besieged_center"),
##           (lt, ":random_no", 0),
##           (party_set_ai_object, ":party_no", ":target_help_besieged_center"),
##           (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
##           (party_set_slot, ":party_no", slot_party_ai_state, spai_helping_town_against_siege),
##           (party_set_slot, ":party_no", slot_party_ai_object, ":target_help_besieged_center"),
##           (try_begin),
##             (is_between, ":center_no", centers_begin, centers_end),
##             (party_detach, ":party_no"),
##           (try_end),
##           (party_set_flags, ":party_no", pf_default_behavior, 1),
##         (else_try),
##           (val_sub, ":random_no", ":chance_patrol_around_center"),
##           (lt, ":random_no", 0),
##           (party_set_ai_object, ":party_no", ":target_patrol_around_center"),
##           (party_set_ai_behavior, ":party_no", ai_bhvr_patrol_party),
##           (party_set_flags, ":party_no", pf_default_behavior, 0),
##           (party_set_slot, ":party_no", slot_party_ai_state, spai_patrolling),
##           (party_set_slot, ":party_no", slot_party_ai_object, ":target_patrol_around_center"),
##           (try_begin),
##             (is_between, ":center_no", centers_begin, centers_end),
##             (party_detach, ":party_no"),
##           (try_end),
##         (try_end),
##       (try_end),
##    ]),
##

  #Party AI: Kingdom Parties thinking after return
  (5,
   [
       (try_for_parties, ":party_no"),
         (party_is_in_any_town, ":party_no"),#TODO: Change this
         (get_party_ai_object, ":party_center", ":party_no"),

         (store_faction_of_party, ":party_faction", ":party_no"),
         (store_faction_of_party, ":center_faction", ":party_no"),
         (try_begin),
           #Redirecting parties because of returning to the newly captured centers
           (neq, ":party_faction", ":center_faction"),
           (party_get_slot, ":party_type", ":party_no", slot_party_type),
           (is_between, ":party_type", kingdom_party_types_begin, kingdom_party_types_end),
           (call_script, "script_get_closest_center_of_faction", ":party_no", ":party_faction"),
           (assign, ":new_center", reg0),
           (party_set_ai_object, ":party_no", ":new_center"),
         (else_try),
           (party_slot_eq, ":party_no", slot_party_ai_state, spai_trading_within_kingdom),
           (call_script, "script_find_travel_location", ":party_center"),
           (assign, ":new_center_no", reg0),
           (try_begin),
             (lt, ":new_center_no", 0),
             (remove_party, ":party_no"),
           (else_try),
             (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
             (party_set_ai_object, ":party_no", ":new_center_no"),
             (party_set_flags, ":party_no", pf_default_behavior, 0),
           (try_end),
         (try_end),
       (try_end),
    ]),

  # Give some xp to hero parties
   (48,
   [
       (try_for_range, ":troop_no", kingdom_heroes_begin, kingdom_heroes_end),
         (store_random_in_range, ":rand", 0, 100),
         (lt, ":rand", 30),
         (troop_get_slot, ":hero_party", ":troop_no", slot_troop_leaded_party),
         (gt, ":hero_party", centers_end),
         (party_is_active, ":hero_party"),
         (party_upgrade_with_xp, ":hero_party", 500),
       (try_end),
    ]),

  

  


  # Decide vassal ai
   (12,
   [
       (try_for_range, ":troop_no", kingdom_heroes_begin, kingdom_heroes_end),
         (call_script, "script_decide_kingdom_party_ai", ":troop_no"),
       (try_end),
    ]),

  
  # Process vassal ai
   (3,
   [
       (call_script, "script_process_kingdom_parties_ai"),
    ]),

  # Process alarms
   (3,
   [
       (call_script, "script_process_alarms"),
    ]),


  # Count faction armies - needs modification
   (24,
   [
       (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
         (call_script, "script_faction_strength", ":faction_no"),
       (try_end),
    ]),

  # Reset hero quest status
  # Change hero relation
  # Change hero favor
#   (24,
#   [
#       (try_for_range, ":troop_no", heroes_begin, heroes_end),
#         (troop_set_slot, ":troop_no", slot_troop_does_not_give_quest, 0),
#         (troop_get_slot, ":relation", ":troop_no", slot_troop_player_relation),
#         (troop_get_slot, ":favor", ":troop_no", slot_troop_player_favor),
#         (try_begin),
#           (gt, ":relation", 0),
#           (try_begin),
#             (gt, ":relation", ":favor"),
#             (store_div, ":increment_amount", ":relation", 10),
#             (val_add, ":increment_amount", 1),
#             (val_add, ":favor", ":increment_amount"),#Moving towards relation by 10% + 1 of the relation every day
#           (else_try),
#             (assign, ":favor", ":relation"),#Favor can not be greater than the relation
#           (try_end),
#         (try_end),
#         (troop_set_slot, ":troop_no", slot_troop_player_favor, ":favor"),
#       (try_end),
#    ]),

  

   (48,
   [
      (try_for_range, ":village_no", villages_begin, villages_end),
        (call_script, "script_refresh_village_defenders", ":village_no"),
      (try_end),
    ]),

  # Accumulate taxes - needs modification
#   (24 * 7,
#   [
#      (try_for_range, ":center_no", centers_begin, centers_end),
#        (party_get_slot, ":accumulated_rents", ":center_no", slot_town_accumulated_rents),
#        (try_begin),
#          (party_slot_eq, ":center_no", slot_party_type, spt_village),
#          (party_slot_eq, ":center_no", slot_village_state, sts_normal),
#          (val_add, ":accumulated_rents", 700),
#        (else_try),
#          (party_slot_eq, ":center_no", slot_party_type, spt_castle),
#          (val_add, ":accumulated_rents", 500),
#        (else_try),
#          (party_slot_eq, ":center_no", slot_party_type, spt_town),
#          (val_add, ":accumulated_rents", 1500),
#        (try_end),
#        (party_set_slot, ":center_no", slot_town_accumulated_rents, ":accumulated_rents"),
#      (try_end),

      #Adding earnings to town lords' wealths.
 #     (try_for_range, ":center_no", centers_begin, centers_end),
 #       (party_get_slot, ":town_lord", ":center_no", slot_town_lord),
 #       (neq, ":town_lord", "trp_player"),
 #       (is_between, ":town_lord", kingdom_heroes_begin, kingdom_heroes_end),
 #       (party_get_slot, ":accumulated_rents", ":center_no", slot_town_accumulated_rents),
 #       (party_get_slot, ":accumulated_tariffs", ":center_no", slot_town_accumulated_tariffs),
 #       (troop_get_slot, ":troop_wealth", ":town_lord", slot_troop_wealth),
 #       (val_add, ":troop_wealth", ":accumulated_rents"),
 #       (val_add, ":troop_wealth", ":accumulated_tariffs"),
 #       (troop_set_slot, ":town_lord", slot_troop_wealth, ":troop_wealth"),
 #       (party_set_slot, ":center_no", slot_town_accumulated_rents, 0),
 #       (party_set_slot, ":center_no", slot_town_accumulated_tariffs, 0),
 #       (assign, reg1, ":troop_wealth"),
 #       (add_troop_note_from_sreg, ":town_lord", 2, "@Current wealth: {reg1}", 0),
 #     (try_end),
 #   ]),

   (7 * 24,
   [
       (call_script, "script_get_number_of_unclaimed_centers_by_player"),
       (assign, ":unclaimed_centers", reg0),
       (gt, ":unclaimed_centers", 0),
# You are holding an estate without a lord.        
#       (try_for_range, ":troop_no", heroes_begin, heroes_end),
#         (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
#         (troop_get_slot, ":relation", ":troop_no", slot_troop_player_relation),
#         (val_sub, ":relation", 1),
#         (val_max, ":relation", -100),
#         (troop_set_slot, ":troop_no", slot_troop_player_relation, ":relation"),
#       (try_end),
# You relation with all kingdoms other than your own has decreased by 1.     
#       (try_for_range, ":faction_no", kingdoms_begin, kingdoms_end),
#         (neq, ":faction_no", "$players_kingdom"),
#         (store_relation,":faction_relation",":faction_no","fac_player_faction"),
#         (val_sub, ":faction_relation", 1),
#         (val_max, ":faction_relation", -100),
#         (set_relation, ":faction_no", "fac_player_faction", ":faction_relation"),
#       (try_end),
    ]),

  
  # Offer player to join faction
#   (24,
#   [
#       (eq, "$players_kingdom", 0),
#       (le, "$g_invite_faction", 0),
#       (eq, "$g_player_is_captive", 0),
#       (troop_get_slot, ":player_renown", "trp_player", slot_troop_renown),
#       (ge, ":player_renown", 100),
#       (store_random_in_range, ":rand", 0, 100),
#       (lt, ":rand", 50),
#       (store_random_in_range, ":kingdom_no", kingdoms_begin, kingdoms_end),
#       (assign, ":min_distance", 999999),
#       (try_for_range, ":center_no", walled_centers_begin, walled_centers_end),
#         (store_distance_to_party_from_party, ":cur_distance", "p_main_party", ":center_no"),
#         (val_min, ":min_distance", ":cur_distance"),
#       (try_end),
#       (lt, ":min_distance", 30),
#       (store_relation, ":reln", ":kingdom_no", "fac_player_faction"),
#       (ge, ":reln", 0),
#       (faction_get_slot, ":kingdom_lord", ":kingdom_no", slot_faction_leader),
#       (troop_get_slot, ":lord_rel", ":kingdom_lord", slot_troop_player_relation),
#       (ge, ":lord_rel", 0),
#       (assign, "$g_invite_offered_center", -1),
#       (try_for_range, ":village_no", villages_begin, villages_end),
#         (store_faction_of_party, ":village_faction", ":village_no"),
#         (eq, ":village_faction", ":kingdom_no"),
#         (assign, "$g_invite_offered_center", ":village_no"),
#       (try_end),
#       (ge, "$g_invite_offered_center", 0),
#       (assign, "$g_invite_faction", ":kingdom_no"),
#       (jump_to_menu, "mnu_invite_player_to_faction"),
#    ]),

  # Attach Lord Parties to the town they are in
#  (0.1,
#   [
#       (try_for_range, ":troop_no", heroes_begin, heroes_end),
#         (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
#         (troop_get_slot, ":troop_party_no", ":troop_no", slot_troop_leaded_party),
#         (ge, ":troop_party_no", 1),
#         (party_get_attached_to, ":cur_attached_town", ":troop_party_no"),
#         (lt, ":cur_attached_town", 1),
#         (party_get_cur_town, ":destination", ":troop_party_no"),
#         (is_between, ":destination", centers_begin, centers_end),
#         (call_script, "script_get_relation_between_parties", ":destination", ":troop_party_no"),
#         (ge, reg0, 0),
#         (party_attach_to_party, ":troop_party_no", ":destination"),
#         (try_begin),
#           (store_faction_of_party, ":troop_faction_no", ":troop_party_no"),
#           (store_faction_of_party, ":destination_faction_no", ":destination"),
#           (eq, ":troop_faction_no", ":destination_faction_no"),
#           (assign, "$g_move_heroes", 1),
#           (call_script, "script_party_prisoners_add_party_prisoners", ":destination", ":troop_party_no"),#Moving prisoners to the center
#           (assign, "$g_move_heroes", 1),
#           (call_script, "script_party_remove_all_prisoners", ":troop_party_no"),
#         (try_end),
#       (try_end),
#    ]),


  # Respawn hero party after kingdom hero is released from captivity.
#  (3,
#   [
#       (try_for_range, ":troop_no", kingdom_heroes_begin, kingdom_heroes_end),
#         (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
#         (troop_slot_eq, ":troop_no", slot_troop_is_prisoner, 0),
#         (neg|troop_slot_ge, ":troop_no", slot_troop_leaded_party, 1),
#
#         (store_troop_faction, ":cur_faction", ":troop_no"),
#         (call_script, "script_cf_select_random_center_with_faction", ":cur_faction"),#Can fail
#         (assign, ":center_no", reg0),
#         (call_script, "script_create_kingdom_hero_party", ":troop_no", ":center_no"),
#         (party_attach_to_party, "$pout_party", ":center_no"),
#       (try_end),
#    ]),

  # Spawn merchant caravan parties
##  (3,
##   [
##       (try_for_range, ":troop_no", merchants_begin, merchants_end),
##         (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_merchant),
##         (troop_slot_eq, ":troop_no", slot_troop_is_prisoner, 0),
##         (neg|troop_slot_ge, ":troop_no", slot_troop_leaded_party, 1),
##
##         (call_script, "script_cf_create_merchant_party", ":troop_no"),
##       (try_end),
##    ]),

  

  
   (24,
   [
  # Updating trade good prices according to the productions
       (call_script, "script_update_trade_good_prices"),
 # Updating player odds
       (try_for_range, ":cur_center", centers_begin, centers_end),
         (party_get_slot, ":player_odds", ":cur_center", slot_town_player_odds),
         (try_begin),
           (gt, ":player_odds", 1000),
           (val_mul, ":player_odds", 95),
           (val_div, ":player_odds", 100),
           (val_max, ":player_odds", 1000),
         (else_try),
           (lt, ":player_odds", 1000),
           (val_mul, ":player_odds", 105),
           (val_div, ":player_odds", 100),
           (val_min, ":player_odds", 1000),
         (try_end),
         (party_set_slot, ":cur_center", slot_town_player_odds, ":player_odds"),
       (try_end),
    ]),


  #Troop AI: Merchants thinking - needs modification (travelling peddler behavior)
  (8,
   [
       (try_for_parties, ":party_no"),
         (party_slot_eq, ":party_no", slot_party_type, spt_kingdom_caravan),
         (party_is_in_any_town, ":party_no"),
         
         (try_begin),
           (party_get_slot, ":cur_ai_state", ":party_no", slot_party_ai_state),
           (eq, ":cur_ai_state", spai_trading_with_town),
           (party_get_cur_town, ":cur_center", ":party_no"),
           (party_get_slot, ":cur_ai_object", ":party_no", slot_party_ai_object),
           (eq, ":cur_center", ":cur_ai_object"),
           (call_script, "script_do_merchant_town_trade", ":party_no", ":cur_center"),
         (try_end),
         
         (try_begin), #Make sure escorted caravan continues to its original destination.
           (eq, "$caravan_escort_party_id", ":party_no"),
           (neg|party_is_in_town, ":party_no", "$caravan_escort_destination_town"),
           (assign, ":target_center", "$caravan_escort_destination_town"),
         (else_try),
           (store_faction_of_party, ":merchant_faction", ":party_no"),
           (call_script, "script_cf_select_random_town_at_peace_with_faction",  ":merchant_faction"),
           (assign, ":target_center", reg0),
         (try_end),
         (neg|party_is_in_town, ":party_no", ":target_center"),
         (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
         (party_set_ai_object, ":party_no", ":target_center"),
         (party_set_flags, ":party_no", pf_default_behavior, 0),
         (party_set_slot, ":party_no", slot_party_ai_state, spai_trading_with_town),
         (party_set_slot, ":party_no", slot_party_ai_object, ":target_center"),
       (try_end),
    ]),

  #Troop AI: Village farmers thinking  - needs modification (travelling farmer behavior)
  (8,
   [
       (try_for_parties, ":party_no"),
         (party_slot_eq, ":party_no", slot_party_type, spt_village_farmer),
         (party_is_in_any_town, ":party_no"),
         (party_get_slot, ":home_center", ":party_no", slot_party_home_center),

         (try_begin),
           (party_is_in_town, ":party_no", ":home_center"),

           (try_begin),
             (store_random_in_range, ":random_no", 0, 100),
             (lt, ":random_no", 10),
             (call_script, "script_do_party_center_trade", ":party_no", ":home_center", 50),
             (assign, ":total_change", reg0),
             (val_div, ":total_change", 10),#10% of the sales.

             #Adding tax revenue to the center
             (party_get_slot, ":accumulated_tariffs", ":home_center", slot_town_accumulated_tariffs),
             (val_add, ":accumulated_tariffs", ":total_change"),
             (party_set_slot, ":home_center", slot_town_accumulated_tariffs, ":accumulated_tariffs"),
      
             #Moving farmers to the home town
             (party_get_slot, ":market_town", ":home_center", slot_village_market_town),
             (party_set_slot, ":party_no", slot_party_ai_object, ":market_town"),
             (party_set_slot, ":party_no", slot_party_ai_state, spai_trading_with_town),
             (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
             (party_set_ai_object, ":party_no", ":market_town"),
             (str_store_party_name, s1, ":home_center"),
             (assign, reg1, ":total_change"),
             (try_begin),
               (eq, "$cheat_mode", 1),
               (display_message, "@Village farmers traded with {s1} merchants. Tax={reg1}"),
             (try_end),
           (try_end),
         (else_try),
           (party_get_slot, ":cur_ai_object", ":party_no", slot_party_ai_object),

           (call_script, "script_do_party_center_trade", ":party_no", ":cur_ai_object", 10),
           (assign, ":total_change", reg0),
           (val_div, ":total_change", 10),#10% of the sales.

           #Adding tax revenue to the center
           (party_get_slot, ":accumulated_tariffs", ":cur_ai_object", slot_town_accumulated_tariffs),
           (val_add, ":accumulated_tariffs", ":total_change"),
           (party_set_slot, ":cur_ai_object", slot_town_accumulated_tariffs, ":accumulated_tariffs"),
           #Adding tax revenue to the home center
           (party_get_slot, ":accumulated_tariffs", ":home_center", slot_town_accumulated_tariffs),
           (val_add, ":accumulated_tariffs", ":total_change"),
           (party_set_slot, ":home_center", slot_town_accumulated_tariffs, ":accumulated_tariffs"),

           #Increasing food stocks of the town
           (party_get_slot, ":town_food_store", ":cur_ai_object", slot_town_food_store),
           (call_script, "script_center_get_food_store_limit", ":cur_ai_object"),
           (assign, ":food_store_limit", reg0),
           (val_add, ":town_food_store", 1000),
           (val_min, ":town_food_store", ":food_store_limit"),
           (party_set_slot, ":cur_ai_object", slot_town_food_store, ":town_food_store"),

           #Moving farmers to their home village
           (party_set_slot, ":party_no", slot_party_ai_object, ":home_center"),
           (party_set_slot, ":party_no", slot_party_ai_state, spai_trading_with_town),
           (party_set_ai_behavior, ":party_no", ai_bhvr_travel_to_party),
           (party_set_ai_object, ":party_no", ":home_center"),
           (str_store_party_name, s1, ":cur_ai_object"),
           (assign, reg1, ":total_change"),
           (try_begin),
             (eq, "$cheat_mode", 1),
             (display_message, "@Village farmers traded with {s1} merchants. Tax={reg1} to both sides"),
           (try_end),
         (try_end),
       (try_end),
    ]),


 



  # Assigning lords to centers with no leaders
#  (72,
#   [(call_script, "script_assign_lords_to_empty_centers"),
#    ]),
  
  

  # Checking escape chances of prisoners that joined the party recently.
  (6,
   [(gt, "$g_prisoner_recruit_troop_id", 0),
    (gt, "$g_prisoner_recruit_size", 0),
    (gt, "$g_prisoner_recruit_last_time", 0),
    (is_currently_night),
    (try_begin),
      (store_skill_level, ":leadership", "skl_leadership", "trp_player"),
      (val_mul, ":leadership", 5),
      (store_sub, ":chance", 66, ":leadership"),
      (gt, ":chance", 0),
      (assign, ":num_escaped", 0),
      (try_for_range, ":unused", 0, "$g_prisoner_recruit_size"),
        (store_random_in_range, ":random_no", 0, 100),
        (lt, ":random_no", ":chance"),
        (val_add, ":num_escaped", 1),
      (try_end),
      (party_remove_members, "p_main_party", "$g_prisoner_recruit_troop_id", ":num_escaped"),
      (assign, ":num_escaped", reg0),
      (gt, ":num_escaped", 0),
      (try_begin),
        (gt, ":num_escaped", 1),
        (assign, reg2, 1),
      (else_try),
        (assign, reg2, 0),
      (try_end),
      (assign, reg1, ":num_escaped"),
      (str_store_troop_name_by_count, s1, "$g_prisoner_recruit_troop_id", ":num_escaped"),
      (display_log_message, "@{reg1} {s1} {reg2?have:has} escaped from your party during the night."),
    (try_end),
    (assign, "$g_prisoner_recruit_troop_id", 0),
    (assign, "$g_prisoner_recruit_size", 0),
    ]),
  


  # Checking center upgrades
#  (12,
#   [(try_for_range, ":center_no", centers_begin, centers_end),
#      (party_get_slot, ":cur_improvement", ":center_no", slot_center_current_improvement),
#      (gt, ":cur_improvement", 0),
#      (party_get_slot, ":cur_improvement_end_time", ":center_no", slot_center_improvement_end_hour),
#      (store_current_hours, ":cur_hours"),
#      (ge, ":cur_hours", ":cur_improvement_end_time"),
#      (party_get_slot, ":cur_level", ":center_no", ":cur_improvement"),
#      (val_add, ":cur_level", 1),
#      (party_set_slot, ":center_no", ":cur_improvement", ":cur_level"),
#      (party_set_slot, ":center_no", slot_center_current_improvement, 0),
#      (try_begin),
#        (eq, ":cur_improvement", slot_center_housing_level),
#        (str_store_string, s3, "@housing"),
#      (else_try),
#        (eq, ":cur_improvement", slot_center_irrigation_level),
#        (str_store_string, s3, "@irrigation"),
#      (else_try),
#        (eq, ":cur_improvement", slot_center_mill_level),
#        (str_store_string, s3, "@mill"),
#      (try_end),
#      (str_store_party_name, s4, ":center_no"),
#      (display_log_message, "@Improvement of {s3} in {s4} has been completed."),
#    (try_end),
#    ]),

  # Asking to give center to player
#  (8,
#   [(assign, ":end_condition", centers_end),
#    (try_for_range, ":center_no", centers_begin, ":end_condition"),
#      (party_slot_eq, ":center_no", slot_town_lord, stl_reserved_for_player),
#      (assign, ":end_condition", 0),
#      (assign, "$g_center_to_give_to_player", ":center_no"),
#      (jump_to_menu, "mnu_give_center_to_player"),
#    (try_end),
#    ]),
  
  # Reduce renown slightly by 0.5% every week
#  (7 * 24,
#   [
#       (troop_get_slot, ":player_renown", "trp_player", slot_troop_renown),
#       (store_div, ":renown_decrease", ":player_renown", 200),
#       (val_sub, ":player_renown", ":renown_decrease"),
#       (troop_set_slot, "trp_player", slot_troop_renown, ":player_renown"),
#    ]),
  
#####!!!!!"""